local game = require "club.threecard.texas"

local M = game.ui.element()

M.store_watchers = {
    ingame = {
        ["结算"] = function(self, store, state, settle_info, init)
            if init then
                return
            end
            self:show(settle_info)
        end,

        ["阶段变更"] = function(self, store, state, args, init)
            if state.phase_id ~= game.store.ingame.state.phases.settle.id then
                self:hide()
            end
        end
    }
}

function M:ctor(ingame)
    self.res = "club.threecard.texas/ui/ingame.b:result_shower"
    self.seats = ingame.seats
    self.public_card = ingame.public_card
end

function M:show(settle_info)
    self:super("show")
    local win_info = nil

    local win_infos = {}
    for _, no in ipairs(settle_info.win_nos) do
        for _, info in ipairs(settle_info.detail) do
            if no == info.no then
                table.insert(win_infos, info)
                break
            end
        end
    end

    if #win_infos == 0 then
        return
    end

    self.name_txt.text = game.i18n.text(string.format("texas:牌型_%s", win_infos[1].rank_class));
    self.public_card:highlight_card(win_infos[1].optimal_card_ids)
    for _, win_info in ipairs(win_infos) do
        local local_no = game.seat.to_local(win_info.no)
        local seat = self.seats[local_no]
        if seat then
            seat:highlight_card(win_info.optimal_card_ids)        
        end
    end
end

return M